<template>
  <div>
    <div class="btn">
      <el-button type="primary" @click="showfrom">添加</el-button>
    </div>
    <list
      :goodList="goodList"
      :total="total"
      :size="size"
      @changePage="changePage"
      @updataGoods="updataGoods"
      @deleteGoodes="deleteGoodes"
    ></list>
    <forms :obj="obj" @addGoods="addGoods" ref="curid" @canle="canle"></forms>
  </div>
</template>

<script>
import list from "./list.vue";
import forms from "./form.vue";
import { goodslist, goodscount } from "../../request/api.js";
export default {
  components: {
    list,
    forms,
  },
  methods: {
    deleteGoodes() {
  
      this.getGoods();
      this.getCount();
    },
    canle() {
      this.obj.isshow = false;
    },
    updataGoods(id) {
      this.showfrom();
      this.$refs.curid.getOneInfo(id);
    },
    addGoods() {
      this.obj.isshow = false;
      this.getGoods();
      this.getCount();
    },
    showfrom() {
      this.obj.isshow = true;
    },
    getGoods() {
      //获取管理员列表信息
      goodslist({ size: this.size, page: this.page })
        .then((d) => {
          if (d.data.code == 200) {
            this.goodList = d.data.list;
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    changePage(curpage) {
      //子组件传过来的页码值 重新调用管理员列表信息
      this.page = curpage;
      this.getCount();
      this.getGoods();
    },
    getCount() {
      goodscount()
        .then((d) => {
          //获取管理员总数
          if (d.data.code == 200) {
            this.total = d.data.list[0].total;
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
  data() {
    return {
      size: 4,
      page: 1,
      goodList: [],
      obj: {
        isshow: false,
      },
      total: 0,
    };
  },
  mounted() {
    this.getGoods();
    this.getCount();
  },
};
</script>

<style>
.btn {
  margin: 30px 0;
}
</style>